package com.finalist.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.finalist.Message;
import com.finalist.Page;
import com.finalist.Pageable;
import com.finalist.entity.Brand;
import com.finalist.entity.CombinedCommodity;
import com.finalist.entity.Product;
import com.finalist.entity.ProductCategory;
import com.finalist.entity.Promotion;
import com.finalist.entity.Tag;
import com.finalist.entity.Product.OrderType;
import com.finalist.entity.Product.RecommendType;
import com.finalist.entity.Tag.Type;
import com.finalist.service.BrandService;
import com.finalist.service.CombinedCommodityService;
import com.finalist.service.ProductCategoryService;
import com.finalist.service.ProductService;
import com.finalist.service.PromotionService;
import com.finalist.service.TagService;

@Controller("combinedCommodityController")
@RequestMapping("/admin/combined_commodity")
public class CombinedCommodityController extends BaseController {

	@Resource(name = "combinedCommodityServiceImpl")
	private CombinedCommodityService combinedCommodityService;
	@Resource(name = "productServiceImpl")
	private ProductService productService;
	@Resource(name = "productCategoryServiceImpl")
	private ProductCategoryService productCategoryService;
	@Resource(name = "brandServiceImpl")
	private BrandService brandService;
	@Resource(name = "promotionServiceImpl")
	private PromotionService promotionService;
	@Resource(name = "tagServiceImpl")
	private TagService tagService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public String list(ModelMap model, Pageable pageable) {
		/*
		 * ProductCategory productCategory =
		 * productCategoryService.find(productCategoryId); Brand brand =
		 * brandService.find(brandId); Promotion promotion =
		 * promotionService.find(promotionId); List<Tag> tags =
		 * tagService.findList(tagId); model.addAttribute("productCategoryTree",
		 * productCategoryService.findTree()); model.addAttribute("brands",
		 * brandService.findAll()); model.addAttribute("promotions",
		 * promotionService.findAll()); model.addAttribute("tags",
		 * tagService.findList(Type.product));
		 * model.addAttribute("productCategoryId", productCategoryId);
		 * model.addAttribute("brandId", brandId);
		 * model.addAttribute("promotionId", promotionId);
		 * model.addAttribute("tagId", tagId);
		 * model.addAttribute("isMarketable", isMarketable);
		 * model.addAttribute("isList", isList); model.addAttribute("isTop",
		 * isTop); model.addAttribute("isGift", isGift);
		 * model.addAttribute("isOutOfStock", isOutOfStock);
		 * model.addAttribute("isStockAlert", isStockAlert);
		 */
		model.addAttribute("page", combinedCommodityService.findPage(null,
				null, null, null, null, null, null, null, null, null, null,
				null, null, OrderType.dateDesc, pageable));
		return "/admin/combined_commodity/list";
	}

	/**
	 * 添加
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Long parentId, ModelMap model) {
		// model.addAttribute("parent", areaService.find(parentId));
		return "/admin/combined_commodity/add";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/selectProductDialog", method = RequestMethod.GET)
	public String selectProductDialog(String type, Long id,
			Long productCategoryId, Long brandId, Long promotionId, Long tagId,
			Boolean isMarketable, Boolean isList, Boolean isTop,
			Boolean isGift, Boolean isOutOfStock, Boolean isStockAlert,
			Pageable pageable, ModelMap model) {
		ProductCategory productCategory = productCategoryService
				.find(productCategoryId);
		Brand brand = brandService.find(brandId);
		Promotion promotion = promotionService.find(promotionId);
		List<Tag> tags = tagService.findList(tagId);
		model.addAttribute("type", type);
		model.addAttribute("id", id);
		model.addAttribute("productCategoryTree",
				productCategoryService.findTree());
		model.addAttribute("brands", brandService.findAll());
		model.addAttribute("promotions", promotionService.findAll());
		model.addAttribute("tags", tagService.findList(Type.product));
		model.addAttribute("productCategoryId", productCategoryId);
		model.addAttribute("brandId", brandId);
		model.addAttribute("promotionId", promotionId);
		model.addAttribute("tagId", tagId);
		model.addAttribute("isMarketable", isMarketable);
		model.addAttribute("isList", isList);
		model.addAttribute("isTop", isTop);
		model.addAttribute("isGift", isGift);
		model.addAttribute("isOutOfStock", isOutOfStock);
		model.addAttribute("isStockAlert", isStockAlert);
		RecommendType recommendType1 = new Product().getRecommendType();
		if (type.equals("duanOne")) {
			recommendType1 = RecommendType.duan_one;
		} else if (type.equals("duanTwo")) {
			recommendType1 = RecommendType.duan_two;
		} else if (type.equals("yunThree")) {
			recommendType1 = RecommendType.yun_third;
		}

		model.addAttribute("page", productService.findPage(productCategory,
				brand, promotion, tags, null, null, null, isMarketable, isList,
				isTop, isGift, isOutOfStock, isStockAlert, OrderType.dateDesc,
				recommendType1, pageable));
		return "/admin/combined_commodity/selectProduct";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/selectProductDialogis", method = RequestMethod.GET)
	public String selectProductDialog(String type, Long id,
			Long productCategoryId, Long brandId, Long promotionId, Long tagId,
			Boolean isMarketable, Boolean isList, Boolean isTop,
			Boolean isGift, Boolean isOutOfStock, Boolean isStockAlert,
			Pageable pageable, Boolean isWei, Boolean isMiao, Boolean isApp,
			ModelMap model) {
		ProductCategory productCategory = productCategoryService
				.find(productCategoryId);
		Brand brand = brandService.find(brandId);
		Promotion promotion = promotionService.find(promotionId);
		List<Tag> tags = tagService.findList(tagId);
		model.addAttribute("type", type);
		model.addAttribute("id", id);
		model.addAttribute("productCategoryTree",
				productCategoryService.findTree());
		model.addAttribute("brands", brandService.findAll());
		model.addAttribute("promotions", promotionService.findAll());
		model.addAttribute("tags", tagService.findList(Type.product));
		model.addAttribute("productCategoryId", productCategoryId);
		model.addAttribute("brandId", brandId);
		model.addAttribute("promotionId", promotionId);
		model.addAttribute("tagId", tagId);
		model.addAttribute("isMarketable", isMarketable);
		model.addAttribute("isList", isList);
		model.addAttribute("isTop", isTop);
		model.addAttribute("isGift", isGift);
		model.addAttribute("isOutOfStock", isOutOfStock);
		model.addAttribute("isStockAlert", isStockAlert);
		RecommendType recommendType1 = new Product().getRecommendType();
		if (type.equals("duanOne")) {
			recommendType1 = RecommendType.duan_one;
		} else if (type.equals("duanTwo")) {
			recommendType1 = RecommendType.duan_two;
		} else if (type.equals("yunThree")) {
			recommendType1 = RecommendType.yun_third;
		}

		Page page = productService.findPage(productCategory, brand, promotion,
				tags, null, null, null, isMarketable, isList, isTop, isGift,
				isOutOfStock, isStockAlert, OrderType.dateDesc, recommendType1,
				isMiao, isWei, isApp, pageable);
		model.addAttribute("page", page);
		return "/admin/combined_commodity/selectProduct";
	}

	/**
	 * huo
	 */
	@RequestMapping(value = "/getProductInfo", method = RequestMethod.POST)
	public @ResponseBody
	List<Product> getProductInfo(Long[] ids, ModelMap model) {
		List<Product> list = productService.findList(ids);
		return list;

	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	Message delete(Long[] ids) {
		combinedCommodityService.delete(ids);
		return SUCCESS_MESSAGE;

	}

	@RequestMapping(value = "/save")
	public String save(CombinedCommodity combinedCommodity, Long[] id1,
			Long[] id2, Long[] id3) {
		/*
		 * , List<Product> products11, List<Product> products22, List<Product>
		 * products33
		 */
		Set<Product> products1 = new HashSet<Product>();
		if (id1 != null) {
			for (int i = 0; i < id1.length; i++) {
				products1.add(productService.find(id1[i]));
			}
		}

		Set<Product> products2 = new HashSet<Product>();
		if (id2 != null) {
			for (int i = 0; i < id2.length; i++) {
				products2.add(productService.find(id2[i]));
			}
		}
		Set<Product> products3 = new HashSet<Product>();
		if (id3 != null) {
			for (int i = 0; i < id3.length; i++) {
				products3.add(productService.find(id3[i]));
			}
		}

		combinedCommodity.setProducts1(products1);
		combinedCommodity.setProducts2(products2);
		combinedCommodity.setProducts3(products3);
		combinedCommodityService.save(combinedCommodity);
		return "redirect:/admin/combined_commodity/list.jhtml";
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {
		CombinedCommodity combinedCommodity = combinedCommodityService.find(id);
		model.addAttribute("combinedCommodity", combinedCommodity);

		return "/admin/combined_commodity/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update")
	public String update(CombinedCommodity combinedCommodity, Long[] id1,
			Long[] id2, Long[] id3) {
		CombinedCommodity old = combinedCommodityService.find(combinedCommodity
				.getId());
		Set<Product> products1 = new HashSet<Product>();
		if (id1 != null) {
			for (int i = 0; i < id1.length; i++) {
				if (old.getProducts1().size() == 0) { // 为空
					products1.add(productService.find(id1[i]));
				} else {
					// for (Product pro : old.getProducts1()) {
					// if (!pro.getId().equals(id1[i])) {
					products1.add(productService.find(id1[i]));
					// }
					// }
				}

			}
		}

		Set<Product> products2 = new HashSet<Product>();
		if (id2 != null) {
			for (int i = 0; i < id2.length; i++) {
				if (old.getProducts2().size() == 0) { // 为空
					products2.add(productService.find(id2[i]));
				} else {
					// for (Product pro : old.getProducts2()) {
					// if (!pro.getId().equals(id2[i])) {
					products2.add(productService.find(id2[i]));
					// }
					// }
				}
			}
		}

		Set<Product> products3 = new HashSet<Product>();
		if (id3 != null) {
			for (int i = 0; i < id3.length; i++) {
				if (old.getProducts3().size() == 0) { // 为空
					products3.add(productService.find(id3[i]));
				} else {
					// for (Product pro : old.getProducts3()) {
					// if (!pro.getId().equals(id3[i])) {
					products3.add(productService.find(id3[i]));
					// }
					// }
				}
			}
		}

		/*
		 * if (products1 != null) { for (Product pro : products1) {
		 * old.getProducts1().add(pro); } }
		 * 
		 * if (products2 != null) { for (Product pro : products2) {
		 * old.getProducts2().add(pro); } }
		 * 
		 * if (products3 != null) { for (Product pro : products3) {
		 * old.getProducts3().add(pro); } }
		 */

		combinedCommodity.setProducts1(products1);
		combinedCommodity.setProducts2(products2);
		combinedCommodity.setProducts3(products3);
		combinedCommodity.setModifyDate(new Date());
		/*
		 * old.setType(combinedCommodity.getType());
		 * old.setPrice(combinedCommodity.getPrice());
		 */
		try {
			combinedCommodityService.update(combinedCommodity);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "redirect:/admin/combined_commodity/list.jhtml";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/listProduct", method = RequestMethod.GET)
	public String list(Long productCategoryId, String type, Long id,
			Long brandId, Long promotionId, Long tagId, Boolean isMarketable,
			Boolean isList, Boolean isTop, Boolean isGift,
			Boolean isOutOfStock, Boolean isStockAlert, Pageable pageable,
			ModelMap model) {
		ProductCategory productCategory = productCategoryService
				.find(productCategoryId);
		Brand brand = brandService.find(brandId);
		Promotion promotion = promotionService.find(promotionId);
		List<Tag> tags = tagService.findList(tagId);
		model.addAttribute("productCategoryTree",
				productCategoryService.findTree());
		model.addAttribute("brands", brandService.findAll());
		model.addAttribute("promotions", promotionService.findAll());
		model.addAttribute("tags", tagService.findList(Type.product));
		model.addAttribute("productCategoryId", productCategoryId);
		model.addAttribute("brandId", brandId);
		model.addAttribute("promotionId", promotionId);
		model.addAttribute("tagId", tagId);
		model.addAttribute("isMarketable", isMarketable);
		model.addAttribute("isList", isList);
		model.addAttribute("isTop", isTop);
		model.addAttribute("type", type);
		model.addAttribute("id", id);
		model.addAttribute("isGift", isGift);
		model.addAttribute("isOutOfStock", isOutOfStock);
		model.addAttribute("isStockAlert", isStockAlert);
		RecommendType recommendType1 = new Product().getRecommendType();
		if (type.equals("duanOne")) {
			recommendType1 = RecommendType.duan_one;
		} else if (type.equals("duanTwo")) {
			recommendType1 = RecommendType.duan_two;
		} else if (type.equals("yunThree")) {
			recommendType1 = RecommendType.yun_third;
		}
		model.addAttribute("page", productService.findPage(productCategory,
				brand, promotion, tags, null, null, null, isMarketable, isList,
				isTop, isGift, isOutOfStock, isStockAlert, OrderType.dateDesc,
				recommendType1, pageable));
		return "/admin/combined_commodity/productInfo";
	}

	/*
	 * @RequestMapping(value = "/getCombinedProductInfo", method =
	 * RequestMethod.POST) public @ResponseBody Set<Product>
	 * getCombinedProductInfo(Long id, String type) { CombinedCommodity
	 * combinedCommodity = combinedCommodityService.find(id); Set<Product>
	 * products = null; if (combinedCommodity!=null){ if
	 * (type.equals("duanOne")) { products = combinedCommodity.getProducts1(); }
	 * else if (type.equals("duanTwo")) { products =
	 * combinedCommodity.getProducts2(); } else if
	 * (type.equals("type=yunThree")) { products =
	 * combinedCommodity.getProducts3(); } }
	 * 
	 * 
	 * return products;
	 * 
	 * }
	 */
	@RequestMapping(value = "/shaicha")
	public @ResponseBody
	Map list6(Long id) {
		CombinedCommodity combinedCommodity = combinedCommodityService.find(id);
		Set<Product> product1 = combinedCommodity.getProducts1();
		Set<Product> productAfter1 = new HashSet<Product>();

		Set<Product> product2 = combinedCommodity.getProducts2();
		Set<Product> productAfter2 = new HashSet<Product>();

		Set<Product> product3 = combinedCommodity.getProducts3();
		Set<Product> productAfter3 = new HashSet<Product>();

		Iterator<Product> iterator1 = product1.iterator();
		while (iterator1.hasNext()) {
			Product product = iterator1.next();
			if (product.getIsWei() != null && product.getIsWei()) {
				productAfter1.add(product);
			}
		}

		Iterator<Product> iterator2 = product2.iterator();
		while (iterator2.hasNext()) {
			Product product = iterator2.next();
			if (product.getIsWei() != null && product.getIsWei()) {
				productAfter2.add(product);
			}
		}

		Iterator<Product> iterator3 = product3.iterator();
		while (iterator3.hasNext()) {
			Product product = iterator3.next();
			if (product.getIsWei() != null && product.getIsWei()) {
				productAfter3.add(product);
			}
		}

		Map map = new HashMap();
		map.put("productAfter1", productAfter1);
		map.put("productAfter2", productAfter2);
		map.put("productAfter3", productAfter3);

		return map;
	}

	@RequestMapping(value = "/shaichaapp")
	public @ResponseBody
	Map list7(Long id) {
		CombinedCommodity combinedCommodity = combinedCommodityService.find(id);
		Set<Product> product1 = combinedCommodity.getProducts1();
		Set<Product> productAfter1 = new HashSet<Product>();

		Set<Product> product2 = combinedCommodity.getProducts2();
		Set<Product> productAfter2 = new HashSet<Product>();

		Set<Product> product3 = combinedCommodity.getProducts3();
		Set<Product> productAfter3 = new HashSet<Product>();

		Iterator<Product> iterator1 = product1.iterator();
		while (iterator1.hasNext()) {
			Product product = iterator1.next();
			if (product.getIsApp() != null && product.getIsApp()) {
				productAfter1.add(product);
			}
		}

		Iterator<Product> iterator2 = product2.iterator();
		while (iterator2.hasNext()) {
			Product product = iterator2.next();
			if (product.getIsApp() != null && product.getIsApp()) {
				productAfter2.add(product);
			}
		}

		Iterator<Product> iterator3 = product3.iterator();
		while (iterator3.hasNext()) {
			Product product = iterator3.next();
			if (product.getIsApp() != null && product.getIsApp()) {
				productAfter3.add(product);
			}
		}

		Map map = new HashMap();
		map.put("productAfter1", productAfter1);
		map.put("productAfter2", productAfter2);
		map.put("productAfter3", productAfter3);

		return map;
	}

}
